package com.baidu.disk.utils;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.Set;

/**
 * Created by CJ on 2018-03-25.
 */
public class BaiduDiskUtil {
    public static WebDriver driver = new ChromeDriver();
    //用户登录
    public static void login(){
        driver.manage().window().maximize();
        System.setProperty("webdriver.chrome.driver", "D:\\Others\\Python\\chromedriver.exe");
        driver.get("https://pan.baidu.com");
        //点击账户密码登录close icon icon-close
        WebElement btn1 = ( new WebDriverWait( driver, 10 )) .until((WebDriver d) -> d.findElement( By.id( "TANGRAM__PSP_4__footerULoginBtn" )));
        btn1.click();
        //输入用户名密码
        WebElement txtbox = driver.findElement(By.name("userName"));
        txtbox.sendKeys("15150863312");
        txtbox = driver.findElement(By.name("password"));
        txtbox.sendKeys("cj1126");
        //点击登录按钮
        btn1 = ( new WebDriverWait( driver, 10 )) .until((WebDriver d) -> d.findElement( By.id( "TANGRAM__PSP_4__submit" )));
        btn1.click();
        //关闭提示框
        //大的提示框
        btn1 = ( new WebDriverWait( driver, 10 )) .until((WebDriver d) -> d.findElement( By.cssSelector("div.close.icon.icon-close")));
        btn1.click();

        //登录成功获得cookie值以及浏览器的sessionId以及浏览器地址
//        driver.getCommandExecutor().getAddressOfRemoteServer().toString();

    }

    //文件上传夹
    public static Integer uploadFolder(Set<Cookie> cookies){

        driver.manage().deleteAllCookies();
        cookies.stream().forEach(cookie -> driver.manage().addCookie(cookie));
        driver.get("https://pan.baidu.com/disk/home?errno=0&errmsg=Auth%20Login%20Sucess&&bduss=&ssnerror=0&traceid=#/all?path=%2F&vmode=list");
        //去除新的界面
        ( new WebDriverWait( driver, 10 )) .until((WebDriver d) -> d.findElement( By.cssSelector("div.know-button>span"))).click();
        //去除登录标签
        //上传文件夹
        driver.findElement(By.id("h5Input2")).sendKeys("F:\\CJ文件\\2017Web\\dao");
        while (true){
            if(driver.getPageSource().contains("文件上传成功")) break;
        }
        return 1;
    }
    //文件上传夹
    public Integer uploadFile(){
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        System.setProperty("webdriver.chrome.driver", "D:\\Others\\Python\\chromedriver.exe");
        driver.get("https://pan.baidu.com");
        //点击账户密码登录
        WebElement btn1 = ( new WebDriverWait( driver, 10 )) .until((WebDriver d) -> d.findElement( By.id( "TANGRAM__PSP_4__footerULoginBtn" )));
        btn1.click();
        //输入用户名密码
        WebElement txtbox = driver.findElement(By.name("userName"));
        txtbox.sendKeys("15150863312");
        txtbox = driver.findElement(By.name("password"));
        txtbox.sendKeys("cj1126");
        //点击登录按钮
        btn1 = ( new WebDriverWait( driver, 10 )) .until((WebDriver d) -> d.findElement( By.id( "TANGRAM__PSP_4__submit" )));
        btn1.click();
        //关闭提示框
        //大的提示框
        btn1 = ( new WebDriverWait( driver, 10 )) .until((WebDriver d) -> d.findElement( By.cssSelector("div.close.icon.icon-close")));
        btn1.click();
        //文件上传
        driver.findElement(By.id("h5Input0")).sendKeys("F:\\1-s2.0-S1877050917329800-main.pdf");
        while (true){
            if(driver.getPageSource().contains("文件上传成功")) break;
        }
        //获取Cookie
        Set<Cookie> cookies = driver.manage().getCookies();
        cookies.parallelStream().forEach(cookie -> {if("BDUSS".equals(cookie.getName()) || "STOKEN".equals(cookie.getName())) System.out.println(cookie.getName()+"============="+cookie.getValue());});
        return 1;
    }

    //获取Cookie
    public String getCookie() throws InterruptedException {
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        System.setProperty("webdriver.chrome.driver", "D:\\Others\\Python\\chromedriver.exe");
        driver.get("https://pan.baidu.com");
        //点击账户密码登录
        WebElement btn1 = ( new WebDriverWait( driver, 10 )) .until((WebDriver d) -> d.findElement( By.id( "TANGRAM__PSP_4__footerULoginBtn" )));
        btn1.click();
        //输入用户名密码
        WebElement txtbox = driver.findElement(By.name("userName"));
        txtbox.sendKeys("15150863312");
        txtbox = driver.findElement(By.name("password"));
        txtbox.sendKeys("cj1126");
        //点击登录按钮
        btn1 = ( new WebDriverWait( driver, 10 )) .until((WebDriver d) -> d.findElement( By.id( "TANGRAM__PSP_4__submit" )));
        btn1.click();
        Thread.sleep(3000);
        //获取Cookie
        String result = "";
        Set<Cookie> cookies = driver.manage().getCookies();
//        cookies.parallelStream().forEach(cookie -> {if("BDUSS".equals(cookie.getName()) || "STOKEN".equals(cookie.getName()))  });

        return "";
    }

    //文件列表


    //文件分享



}
